﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace QLNSClient
{
    public partial class Frm_PCTN : DevExpress.XtraEditors.XtraForm
    {

        QLNSServiceReference.QLNSServiceClient sv = new QLNSServiceReference.QLNSServiceClient();
                
        public Frm_PCTN()
        {
            InitializeComponent();
        }



        public void LoadData()
        {
            btn_Luu.Enabled = false;
            btn_Xoa.Enabled = false;
            btn_Sua.Enabled = false;
            btn_Huy.Enabled = false;
            
            gv_dsPCTN.DataSource = sv.getPCTN();
            gridView1.Columns[0].Caption = "Mã phụ cấp thâm niên";
            gridView1.Columns[1].Caption = "Tên phụ cấp thâm niên";
            gridView1.Columns[2].Caption = "Giá trị (%)";

            txt_MaPCTN.Enabled = false;
            txt_TenPCTN.Enabled = false;
            txt_HeSoPCTN.Enabled = false;
        }

        private void Frm_PCTN_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void btn_Them_Click(object sender, EventArgs e)
        {
            btn_Luu.Enabled = true;
            btn_Xoa.Enabled = false;
            btn_Sua.Enabled = false;
            btn_Huy.Enabled = true;
            
            List<QLNSServiceReference.PHUCAP_THAMNIEN> list = new List<QLNSServiceReference.PHUCAP_THAMNIEN>(sv.getLastPCTN());
            int lastID = list[0].Ma_PCTN;
            txt_MaPCTN.Text = (lastID + 1).ToString();
            
            txt_MaPCTN.Enabled = false;
            txt_TenPCTN.Enabled = true;
            txt_HeSoPCTN.Enabled = true;

            txt_TenPCTN.Text = "";
            txt_HeSoPCTN.Text = "";
        }

        private void btn_Luu_Click(object sender, EventArgs e)
        {
            btn_Huy.Enabled = false;
            btn_Them.Enabled = true;

            string tempMaPCTN = txt_MaPCTN.Text;
            string tempTenPCTN = txt_TenPCTN.Text;
            string tempHS = txt_HeSoPCTN.Text;

            QLNSServiceReference.PHUCAP_THAMNIEN pctn = new QLNSServiceReference.PHUCAP_THAMNIEN();
            pctn.Ma_PCTN = Int32.Parse(tempMaPCTN);
            pctn.TenPCTN = tempTenPCTN;
            if (tempHS == "")
            {
                pctn.HeSoPCTN = Int32.Parse(tempMaPCTN);
            }
            else
            {
                pctn.HeSoPCTN = Int32.Parse(tempHS);
            }
            if (demsua == 1)
            {
                DialogResult dr = MessageBox.Show("Bạn có chắc muốn cập nhật lại thông tin!", "Cảnh báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                if (dr == DialogResult.OK)
                {
                    sv.updatePCTN(pctn);
                    LoadData();
                    demsua = 0;
                    MessageBox.Show("Cập nhật thông tin thâm niên mã '"+txt_MaPCTN.Text+"' thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
            {
                int kq = sv.addPCTN(pctn);
                if (kq != -1)
                {
                    LoadData();
                    MessageBox.Show("Thêm hệ số thâm niên '"+txt_MaPCTN.Text+"' thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("Loại phụ cấp thâm niên đã tồn tại! Vui lòng xem lại!", "Cảnh báo");
                }
            }

        }

        private void btn_Huy_Click(object sender, EventArgs e)
        {
            demsua = 0;

            btn_Luu.Enabled = false;
            btn_Huy.Enabled = false;
            btn_Them.Enabled = true;
            btn_Xoa.Enabled = false;
            btn_Sua.Enabled = false;

            txt_MaPCTN.Enabled = false;
            txt_TenPCTN.Enabled = false; 
            txt_HeSoPCTN.Enabled = false;
        }

        int demsua = 0;
        private void btn_Sua_Click(object sender, EventArgs e)
        {
            demsua = 1;

            btn_Luu.Enabled = true;
            btn_Them.Enabled = false;
            btn_Huy.Enabled = true;
            btn_Sua.Enabled = false;

            txt_TenPCTN.Enabled = true;
            txt_HeSoPCTN.Enabled = true;
        }

        private void btn_Xoa_Click(object sender, EventArgs e)
        {
            btn_Them.Enabled = true;
            btn_Luu.Enabled = false;
        }

        private void gv_dsPCTN_Click(object sender, EventArgs e)
        {
            btn_Luu.Enabled = false;
            btn_Xoa.Enabled = true;
            btn_Sua.Enabled = true;
            btn_Huy.Enabled = false;
            btn_Them.Enabled = true;

            txt_MaPCTN.Enabled = false;
            txt_TenPCTN.Enabled = false;
            txt_HeSoPCTN.Enabled = false;
            try
            {
                txt_MaPCTN.Text = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[0]);
                txt_TenPCTN.Text = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[1]);
                txt_HeSoPCTN.Text = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[2]);
            }
            catch { }
        }
    }
}
